<?php
// 仅当会话尚未启动时调用 session_start()
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
// 检查是否为读者角色
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'reader') {
    header('Location: http://localhost/Library_management_system/index.php');
    exit();
}

try {
    $pdo = new PDO('mysql:host=localhost;dbname=library', 'root', '123456');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("数据库连接失败: " . $e->getMessage());
}

// 处理搜索请求
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $search = $_POST['search'];

    // 构建查询，支持通过图书编号、图书名称、ISBN进行搜索
    $stmt = $pdo->prepare('
        SELECT b.book_No, b.B_name, b.ISBN, b.Recommendation_degree, a.author_name, c.Cate_name, p.P_name AS Publisher_name
        FROM book b
        LEFT JOIN book_cate bc ON b.book_No = bc.book_No
        LEFT JOIN category c ON bc.Cate_No = c.Cate_No
        LEFT JOIN authors a ON b.book_No = a.author_id
        LEFT JOIN publisher p ON b.P_ID = p.P_ID
        WHERE b.book_No LIKE ? OR b.B_name LIKE ? OR b.ISBN LIKE ?
    ');
    $stmt->execute(['%' . $search . '%', '%' . $search . '%', '%' . $search . '%']);
    $books = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>查询图书</title>
</head>

<body>
    <h2>查询图书</h2>

    <!-- 搜索表单 -->
    <form method="POST">
        <label for="search">请输入图书编号、图书名称或 ISBN：</label>
        <input type="text" name="search" required>
        <button type="submit">查询</button>
    </form>

    <?php if (isset($books)): ?>
        <h3>查询结果：</h3>
        <ul>
            <?php if (count($books) > 0): ?>
                <?php foreach ($books as $book): ?>
                    <?php
                    // 查询该图书是否有未归还的借阅记录
                    $stmt = $pdo->prepare('
                        SELECT COUNT(*) FROM Loan WHERE book_No = ? AND state_ID = "未还"
                    ');
                    $stmt->execute([$book['book_No']]);
                    $borrowed = $stmt->fetchColumn();

                    // 判断是否可借阅
                    $is_available = ($borrowed == 0) ? '可借阅' : '不可借阅';

                    // 获取该图书的最近评论
                    $stmt = $pdo->prepare('
                        SELECT u_No, Re_score, Re_pre FROM bookcomment
                        WHERE book_No = ? ORDER BY Re_ID DESC LIMIT 1
                    ');
                    $stmt->execute([$book['book_No']]);
                    $comment = $stmt->fetch(PDO::FETCH_ASSOC);
                    ?>
                    <li>
                        <strong>图书编号:</strong> <?php echo htmlspecialchars($book['book_No'] ?? ''); ?>,
                        <strong>图书名称:</strong> <?php echo htmlspecialchars($book['B_name'] ?? ''); ?>,
                        <strong>ISBN:</strong> <?php echo htmlspecialchars($book['ISBN'] ?? ''); ?>,
                        <strong>推荐度:</strong> <?php echo htmlspecialchars($book['Recommendation_degree'] ?? ''); ?>,
                        <strong>作者:</strong> <?php echo htmlspecialchars($book['author_name'] ?? '未知'); ?>,
                        <strong>类别:</strong> <?php echo htmlspecialchars($book['Cate_name'] ?? '未分类'); ?>,
                        <strong>出版社:</strong> <?php echo htmlspecialchars($book['Publisher_name'] ?? '未知出版社'); ?>
                        <form method="GET" action="bookContent.php" style="display:inline;">
                                <button type="submit" name="book_No"
                                    value="<?php echo htmlspecialchars($book['book_No']); ?>">查看详情</button>
                            </form>

                        


                    </li>

                    
                    
                <?php endforeach; ?>
            <?php else: ?>
                <li>没有找到匹配的图书。</li>
            <?php endif; ?>
        </ul>
    <?php endif; ?>
</body>

</html>